<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-row :gutter="10" type="flex">
        <el-form-item label="管理员账号" prop="login_name">
          <el-input v-model="formData.login_name" placeholder="请输入" size="small" style="width:300px" />
        </el-form-item>
        <el-form-item label="管理员姓名" prop="name">
          <el-input
            v-model="formData.name"
            size="small"
            style="width:300px"
            :rows="3"
            placeholder="请输入"
          />
        </el-form-item>
      </el-row>
      <el-row :gutter="10" type="flex">
        <el-form-item label="管理员邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入" size="small" style="width:300px" />
        </el-form-item>
        <el-form-item label="管理员手机号" prop="phone">
          <el-input
            v-model="formData.phone"
            size="small"
            style="width:300px"
            :rows="3"
            placeholder="请输入"
          />
        </el-form-item>
      </el-row>
      <el-row :gutter="10" type="flex">
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="formData.password" placeholder="请输入" size="small" style="width:300px" />
        </el-form-item>
        <el-form-item label="账号状态" prop="status">
          <el-switch
            v-model="formData.status"
            active-text="冻结"
            inactive-text="启用"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>

      </el-row>
      <el-form-item label="角色">
        <template>
          <el-checkbox-group v-model="formData.role_id" @change="changecheCkout">
            <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
        </template>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { getsysroleListApi, addsysuserInfoApi, updatesysuserInfoApi } from '@/api/sys_user'
export default {
  name: 'User',
  data() {
    return {
      // checkList: [],
      list: [], // 所有权限
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        login_name: '', // 账号
        name: '', // 姓名
        email: '', // 邮箱
        phone: '', // 手机号
        // rolename: '', // 角色
        role_id: [], // 权限，上传权限id[数组类型]
        status: '', // 状态
        password: '' // 密码，不填为空
      },
      // 表单验证规则
      formRules: {
        advposdesc: [{ required: true, message: '请输入', trigger: 'blur' }],
        advposname: [{ required: true, message: '请输入', trigger: 'blur' }],
        advpossize: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    this.getsysroleListApi()
  },
  methods: {
    // 点击复选框
    changecheCkout() {
      console.log(this.checkList)
    },
    // 查询所有的权限
    async getsysroleListApi() {
      const res = await getsysroleListApi()
      this.list = res.data
      console.log(res.data)
    },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updatesysuserInfoApi(this.formData)
        : await addsysuserInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        login_name: '', // 账号
        name: '', // 姓名
        email: '', // 邮箱
        phone: '', // 手机号
        // rolename: '', // 角色
        role_id: [], // 权限，上传权限id[数组类型]
        status: '', // 状态
        password: '' // 密码，不填为空
      }
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style scoped></style>
